package com.css.apps.base.common.tree.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.css.apps.base.common.provider.OrgQueryProvider;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.Environment;
import com.css.db.util.StringHelper;
import com.css.util.Ajax;
import com.css.webbase.webwork.action.AbstractAction;

@SuppressWarnings("serial")
public class GetOrgUserAndPostJson extends AbstractAction {
	// 机构Id
	private String orgId;
	private String orgName;
	private boolean showPost = true;
	private boolean showActor = true;

	protected String go() {
		if (StringHelper.isEmpty(orgId)) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, "OrgId is  missing!");
		} else {
			orgName = OrgQueryProvider.getOrg(orgId.trim()).getOrgName();
			JSONArray user_post_array = new JSONArray();
			if (showPost) {
				java.util.List<SPost> post = OrgQueryProvider.queryPost(orgId);
				// Post , type ='GROUP'
				addPost(post, user_post_array);
			}
			if (showActor || !showPost) {
				java.util.List<SUser> user = OrgQueryProvider.queryOrgBizUserList(orgId.trim());
				// Person,type = 'ACTOR'
				addPerson(user, user_post_array);
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, "success!", user_post_array);
		}
		return SUCCESS;
	}

	private void addPost(java.util.List<SPost> posts, JSONArray arr) throws JSONException {
		JSONObject one;
		List<Object[]> relateIdAndOrgIdList = OrgQueryProvider.checkRelate("2");
		String relateId = null;
		String relateOrgId = null;
		if (relateIdAndOrgIdList != null) {
			for (Object[] Ids : relateIdAndOrgIdList) {
				relateId += (String) Ids[0] + ",";
				relateOrgId += (String) Ids[1] + ",";
			}
		}
		for (SPost post : posts) {
			one = new JSONObject();
			one.put("id", post.getPostId());
			one.put("name", post.getName() + "[岗]/" + orgName);
			one.put("org", orgId.trim());
			// one.put("typeid",
			// WorkFlowConstant.GROUP+"/"+orgId.trim()+"/"+post.getPostId()+"/"+WorkFlowConstant.EMPTY_PERFORMER_PART+"/"+post.getName());
			one.put("typeid", "GROUP/" + orgId.trim() + "/" + post.getPostId() + "/_");
			// one.put("relateFlag",
			// OrgProvider.checkRelate(post.getPostId(),"2"));
			if (relateId != null && relateOrgId != null && relateId.indexOf(post.getPostId()) != -1
					&& relateOrgId.indexOf(orgId.trim()) != -1) {
				one.put("relateFlag", true);
			} else {
				one.put("relateFlag", false);
			}
			arr.add(one);
		}
	}

	private void addPerson(java.util.List<SUser> users, JSONArray arr) throws JSONException {
		JSONObject one;
		List<Object[]> relateIdAndOrgIdList = OrgQueryProvider.checkRelate("1");
		String relateId = null;
		String relateOrgId = null;
		if (relateIdAndOrgIdList != null) {
			for (Object[] Ids : relateIdAndOrgIdList) {
				relateId += (String) Ids[0] + ",";
				relateOrgId += (String) Ids[1] + ",";
			}
		}
		for (SUser user : users) {
			one = new JSONObject();
			one.put("id", user.getUserId());
			one.put("name", user.getRealName() + "[人]/" + orgName);
			one.put("org", orgId.trim());
			one.put("typeid", "ACTOR/" + orgId.trim() + "/" + "_/" + user.getUserId());
			// one.put("relateFlag",
			// OrgProvider.checkRelate(user.getUserId(),"1"));
			if (relateId != null && relateOrgId != null && relateId.indexOf(user.getUserId()) != -1
					&& relateOrgId.indexOf(orgId.trim()) != -1) {
				one.put("relateFlag", true);
			} else {
				one.put("relateFlag", false);
			}
			arr.add(one);
		}
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public boolean isShowPost() {
		return showPost;
	}

	public void setShowPost(boolean showPost) {
		this.showPost = showPost;
	}

	public boolean isShowActor() {
		return showActor;
	}

	public void setShowActor(boolean showActor) {
		this.showActor = showActor;
	}
}